我目前正在使用MediaPlayer播放一首可以播放的歌曲。在用户选择不同的歌曲时,我正试图将其播出。 但是,我得到MediaPlayer的错误(1,-2147483648)和错误(1,-2147483648)。 调用prepareASync()后会打印这些日志; 注意:文件的路径看起来正确。但是,如何检查有效的文件路径?
顺序如下:
mPlayer.reset();
Log.d(TAG,"after reset");
mPlayer.setDataSource(mTrackToBePlayed);
Log.d(TAG,"after setDataSource");
// mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener(preparedListener);
Log.d(TAG,"after setOnPreparedListener");
mPlayer.setOnErrorListener(errorListener);
Log.d(TAG,"after setting errorListener");
mPlayer.prepareAsync();
Log.d(TAG,"after prepareAsync");
mPlayer.reset();
Log.d(TAG,"after reset");
mPlayer.setDataSource(mTrackToBePlayed);
Log.d(TAG,"after setDataSource");
// mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener(preparedListener);
Log.d(TAG,"after setOnPreparedListener");
mPlayer.setOnErrorListener(errorListener);
Log.d(TAG,"after setting errorListener");
mPlayer.prepareAsync();
Log.d(TAG,"after prepareAsync");
引用的代码是这样的:在我的主要活动的onResume()中(在返回时,我想开始播放)我正在调用MusicUtils.createPlayer();和 MusicUtils.playTrack(mCurrentTrack);
public static void playTrack(String track) {
mTrackToBePlayed = track;
try {
mPlayer.reset();
Log.d(TAG,"after reset");
mPlayer.setDataSource(mTrackToBePlayed);
Log.d(TAG,"after setDataSource");
// mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener(preparedListener);
Log.d(TAG,"after setOnPreparedListener");
mPlayer.setOnErrorListener(errorListener);
Log.d(TAG,"after setting errorListener");
mPlayer.prepareAsync();
Log.d(TAG,"after prepareAsync");
} catch (IOException e) {
} catch(IllegalArgumentException e) {
}
preparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
Log.d(TAG,"after start");
}
};
errorListener = new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Log.d(TAG,"in onError");
// mIsInitialized = false;
mp.release();
// Creating a new MediaPlayer and settings its wakemode does not
// require the media service, so it's OK to do this now, while the
// service is still being restarted
try{
mp = new MediaPlayer();
mp.reset();
mp.setDataSource(mTrackToBePlayed);
// mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepareAsync();
mPlayer = mp;
// mPlayer.start();
} catch (Exception e) {
}
//MediaPlayer.create(mContext, mTrackToBePlayed);
// mPlayer.setWakeMode(MediaPlaybackService.this, PowerManager.PARTIAL_WAKE_LOCK);
return true;
default:
Log.d("MusicPlayer", "Error: " + what + "," + extra);
break;
}
return false;
}
};
//mPlayer.start();
}
public static boolean isPlaying() {
return mPlayer.isPlaying();
}
public static void pauseTrack() {
mPlayer.pause();
}
public static void startTrack() {
mPlayer.start();
}
public static void stopTrack() {
mPlayer.stop();
}
public static void releasePlayer() {
mPlayer.release();
}
public static void playTrack(String track) {
mTrackToBePlayed = track;
try {
mPlayer.reset();
Log.d(TAG,"after reset");
mPlayer.setDataSource(mTrackToBePlayed);
Log.d(TAG,"after setDataSource");
// mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener(preparedListener);
Log.d(TAG,"after setOnPreparedListener");
mPlayer.setOnErrorListener(errorListener);
Log.d(TAG,"after setting errorListener");
mPlayer.prepareAsync();
Log.d(TAG,"after prepareAsync");
} catch (IOException e) {
} catch(IllegalArgumentException e) {
}
preparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
Log.d(TAG,"after start");
}
};
errorListener = new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Log.d(TAG,"in onError");
// mIsInitialized = false;
mp.release();
// Creating a new MediaPlayer and settings its wakemode does not
// require the media service, so it's OK to do this now, while the
// service is still being restarted
try{
mp = new MediaPlayer();
mp.reset();
mp.setDataSource(mTrackToBePlayed);
// mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepareAsync();
mPlayer = mp;
// mPlayer.start();
} catch (Exception e) {
}
//MediaPlayer.create(mContext, mTrackToBePlayed);
// mPlayer.setWakeMode(MediaPlaybackService.this, PowerManager.PARTIAL_WAKE_LOCK);
return true;
default:
Log.d("MusicPlayer", "Error: " + what + "," + extra);
break;
}
return false;
}
};
//mPlayer.start();
}
public static boolean isPlaying() {
return mPlayer.isPlaying();
}
public static void pauseTrack() {
mPlayer.pause();
}
public static void startTrack() {
mPlayer.start();
}
public static void stopTrack() {
mPlayer.stop();
}
public static void releasePlayer() {
mPlayer.release();
}
This function is present in MusicUtils and is used to obtain the first track that the cursor returns
public static void retrieveDefaultPath() {
String[] STAR = { "*" };
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
mCursor = mContentResolver.query(allsongsuri, STAR, selection, null, null);
if (mCursor != null) {
if (mCursor.moveToFirst()) {
//do {
//mSongName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
//mSongList.add(mSongName);//populate the list of display names
int song_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
//mMusicIDs.put(song_id, counter++);//fill the HashMap with IDs corresponding to the positions
String fullpath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String album_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
//} while (cursor.moveToNext());
mDefaultPath = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + "/" + song_id;
Slideshow.mCurrentTrack = mDefaultPath;
}
if (mCursor != null) {
mCursor.close();
}
}
}
public static void retrieveDefaultPath() {
String[] STAR = { "*" };
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
mCursor = mContentResolver.query(allsongsuri, STAR, selection, null, null);
if (mCursor != null) {
if (mCursor.moveToFirst()) {
//do {
//mSongName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
//mSongList.add(mSongName);//populate the list of display names
int song_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
//mMusicIDs.put(song_id, counter++);//fill the HashMap with IDs corresponding to the positions
String fullpath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String album_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
//} while (cursor.moveToNext());
mDefaultPath = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + "/" + song_id;
Slideshow.mCurrentTrack = mDefaultPath;
}
if (mCursor != null) {
mCursor.close();
}
}
}
logcat的:
08-31 18:07:36.348:D / dalvikvm(23090):GC_EXTERNAL_ALLOC释放2K,46%免费3088K / 5703K,外部1461K / 1538K,暂停32ms
08-31 18:07:38.860:D / tag(23090):在oncreate里面
08-31 18:07:38.860:D / ImageSwitcher(23090):缓存大小:1
08-31 18:07:39.030:D / ImageSwitcher(23090):总计数字:838
08-31 18:07:39.150:I / AudioSystem(23090):获得音频flinger
08-31 18:07:39.150:I / AudioSystem(23090):返回新的音频会话ID
08-31 18:07:39.150:D / IAudioFlinger(23090):newAudioSessionId In
08-31 18:07:39.150:D / IAudioFlinger(23090):newAudioSessionId Out,id = 175
08-31 18:07:39.150:D / MediaPlayer(23090):reset()in
08-31 18:07:39.150:D / MediaPlayer(23090):reset()out
08-31 18:07:39.150:D / MusicUtils(23090):重置后
08-31 18:07:39.160:D / MusicUtils(23090):在setDataSource之后
08-31 18:07:39.160:D / MusicUtils(23090):在setOnPreparedListener之后
08-31 18:07:39.160:D / MusicUtils(23090):设置errorListener之后
08-31 18:07:39.170:D / MusicUtils(23090):在prepareAsync之后
08-31 18:07:39.250:D / MediaPlayer(23090):start()in
08-31 18:07:39.260:D / MediaPlayer(23090):start()out
08-31 18:07:39.270:D / MediaPlayer(23090):start()in
08-31 18:07:39.270:D / MediaPlayer(23090):start()out
08-31 18:07:39.270:D / MusicUtils(23090):开始后
08-31 18:07:49.340:D / MediaPlayer(23090):暂停()
08-31 18:07:49.340:D / MediaPlayer(23090):暂停() 08-31 18:07:49.560:D / dalvikvm(23090):GC_CONCURRENT释放239K,45%免费3260K / 5831K,外部494K / 1006K,暂停10ms + 7ms
08-31 18:07:50.842:D /音乐列表(23090):在getAllSongsFromSDCARD之前的onCreate
08-31 18:07:50.922:D / TAG(23090):设置适配器
08-31 18:07:50.922:D / TAG(23090):设置适配器
08-31 18:07:51.953:D / dalvikvm(23090):GC_EXTERNAL_ALLOC释放146K,45%免费3264K / 5831K,外部794K / 855K,暂停29ms
08-31 18:07:53.905:D / MusicList(23090):在setItemChecked之后
08-31 18:07:54.585:D / Settings(23090):onActivityResult内部
08-31 18:07:55.476:W / KeyCharacterMap(23090):无法打开keycharmap文件
08-31 18:07:55.476:W / KeyCharacterMap(23090):加载keycharmap时出错
file'/system/usr/keychars/cy8c-touchscreen.kcm.bin'。 hw.keyboards.65538.devname = 'cy8c-触摸屏' 08-31 18:07:55.476:I / KeyCharacterMap(23090):使用默认值 keymap:/system/usr/keychars/qwerty.kcm.bin
08-31 18:07:55.486:D / MediaPlayer(23090):reset()in
08-31 18:07:55.486:D / MediaPlayer(23090):reset()out
08-31 18:07:55.486:D / MusicUtils(23090):重置后
08-31 18:07:55.486:D / MusicUtils(23090):在setDataSource之后
08-31 18:07:55.486:D / MusicUtils(23090):在setOnPreparedListener之后
08-31 18:07:55.486:D / MusicUtils(23090):设置errorListener之后
08-31 18:07:55.486:D / MusicUtils(23090):在prepareAsync之后
08-31 18:07:55.496:E / MediaPlayer(23090):错误(1,-2147483648)
08-31 18:07:55.506:E / MediaPlayer(23090):错误(1,-2147483648)
08-31 18:07:55.506:D / MusicPlayer(23090):错误:1,-2147483648
答案 0 :(得分:0)
**Check with the following class it will list out all th songs in the device also it play the song when click on that name.**
public class NewMp3PlayerActivity extends Activity {
ListView musiclist;
int music_column_index,music_column_index1;
int count;
MediaPlayer mMediaPlayer;
ImageView play, pause, forward, backward;
int pposition;
ArrayList<String> songs = new ArrayList<String>(); String songName; Cursor musiccursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songpreview);
play = (ImageView)findViewById(R.id.img_play);
pause = (ImageView)findViewById(R.id.img_pause);
forward = (ImageView)findViewById(R.id.img_forward);
backward = (ImageView)findViewById(R.id.img_backward);
play.setOnClickListener(OnClick);
pause.setOnClickListener(OnClick);
forward.setOnClickListener(OnClick);
backward.setOnClickListener(OnClick);
init_phone_music_grid();
}
View.OnClickListener OnClick = new View.OnClickListener() {
@Override public void onClick(View v) { music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Log.e("", "musiccursor.getCount()-->"musiccursor.getCount()); switch(v.getId()){ case R.id.img_play:
Log.v("", "list of songs-->"+songs);
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.start();
break; case R.id.img_pause:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
break; case R.id.img_forward: // musiccursor.moveToPosition(pposition+1);
try {
String filename="";
if (pposition < songs.size()){
pposition = pposition+1;
filename = songs.get(pposition);//musiccursor.getString(music_column_index);
}else{
}
Log.e("", "filename-->"+filename);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException ie){
pposition = 0;
}
break; case R.id.img_backward:
try {
String filename_n="";
if (pposition <= songs.size()){
pposition = pposition-1;
filename_n = songs.get(pposition);//musiccursor.getString(music_column_index);
}else{
}
mMediaPlayer.reset();
mMediaPlayer.setDataSource(filename_n);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(ArrayIndexOutOfBoundsException ar){
pposition = songs.size();
}
break; default:
break; }
} };
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, proj, null, null, null);
Log.e("", "filename-->"musiccursor.getCount());
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.lst_music);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
try {
String filename="";
if (pposition < songs.size()){
pposition = pposition+1;
filename = songs.get(pposition);//musiccursor.getString(music_column_index);
}else{
}
Log.e("", "filename-->"+filename);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException ie){
pposition = 0;
}
} });
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
System.gc();
pposition = position;
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
String filen = songs.get(pposition);
Log.e("", "index-->"+music_column_index);
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(filen);
mMediaPlayer.prepare(); // mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
music_column_index1 = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
songName = musiccursor.getString(music_column_index);
songs.add( musiccursor.getString(music_column_index1) );
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
} }
答案 1 :(得分:0)
序列正确。路径出错了。正在使用硬编码的字符串而不是Uri。感谢。