abdelrhman
abdelrhman

Reputation: 133

Why Sound in MediaPlayer does not Play?

The problem is that the sound doesn't play.

When I run the application this sentence appear in (Android Mointor)" D/AbsListView: unregisterIRListener() is called "

the MainActivity.java code

public class MainActivity extends AppCompatActivity {
     @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
        ArrayList<String> list = new ArrayList<String>();
        list.add("item1");


        //instantiate custom adapter
        List adapter = new List(list, this);

        //handle listview and assign adapter
        ListView lView = (ListView)findViewById(R.id.listView);
        lView.setAdapter(adapter);
    }

}

the List.java code:

public class List extends BaseAdapter implements ListAdapter {
    private ArrayList<String> list = new ArrayList<String>();
    private Context context;
     MediaPlayer mediaPlayer;


    public List(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.activity_list, null);
        }

        //Handle TextView and display string from your list
        TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position));

        //Handle buttons and add onClickListeners

        Button addBtn = (Button)view.findViewById(R.id.add_btn);


        addBtn.setOnClickListener(new View.OnClickListener(){


            @Override
            public void onClick(View v) {
                  MediaPlayer.create(List.this.context, R.raw.people)
                        mediaPlayer.start();
                notifyDataSetChanged();
            }
        });

        return view;
    }
}

Upvotes: 2

Views: 68

Answers (3)

Dang Nguyen
Dang Nguyen

Reputation: 354

You can use the below method to play a sound. It can be played from storage or url from Internet.

public void playAudio(String audioPath) {
    try {
        MediaPlayer mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setDataSource(audioPath);
        mp.prepare();
        mp.start();
        Log.d(TAG, "is playing audio...");
    } catch (Exception e) {
        Log.d(TAG, "cannot play audio: " + e.toString());
    }
}

Upvotes: 1

Sagar Jogadia
Sagar Jogadia

Reputation: 1350

Try this:

@Override
public void onClick(View v) {
    mediaPlayer = MediaPlayer.create(context,R.raw.people);
    mediaPlayer.start();
    notifyDataSetChanged();
}

Upvotes: 1

Iris Louis
Iris Louis

Reputation: 297

I think you must be changed this to context

 addBtn.setOnClickListener(new View.OnClickListener(){


        @Override
        public void onClick(View v) {
            mediaPlayer.create(context,R.raw.people);
            mediaPlayer.start();
            notifyDataSetChanged();
        }
    });

Upvotes: 1

Related Questions