播放 SD 卡中的歌曲时搜索栏不工作

发布于 2024-11-07 06:36:14 字数 2495 浏览 1 评论 0原文

在我的应用程序中,我尝试播放媒体播放器和搜索栏。当我尝试从应用程序中存储的文件夹中播放歌曲时,它工作正常并且搜索栏被移动。

当我尝试从 SD 卡播放连续歌曲时,它崩溃了,并且在 logcat 中显示以下错误。

ERROR/sdcard play(2500): 02 - bird flu.mp3
ERROR/MediaPlayer(2500): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
ERROR/MediaPlayer(2500): error (1, -4)
ERROR/MediaPlayer(2500): Attempt to call getDuration without a valid mediaplayer
ERROR/MediaPlayer(2500): error (-38, 0)
ERROR/sdcard play(2500): 03 Un Mela Aasadhaan.mp3
ERROR/MediaPlayer(2500): setDataSource called in state 0
ERROR/MediaPlayer(2500): prepareAsync called in state 0
ERROR/MediaPlayer(2500): Attempt to call getDuration without a valid mediaplayer
ERROR/MediaPlayer(2500): error (-38, 0)

以下是我的代码

 sv = (SurfaceView)findViewById(R.id.surfaceView1);
//        mediaPlayer =MediaPlayer.create(this, R.raw.b);        
init_mediaPlayer();     


// sv.postDelayed(runnable,200);                
  seek = (SeekBar)findViewById(R.id.seekBar1);


 seek.setMax(mediaPlayer.getDuration());

System.out.println("**** MEDIA PLAYER DURATION "+ mediaPlayer.getDuration());       


play.setOnClickListener(new View.OnClickListener() 

{
public void onClick(View v) 

{

mediaPlayer.start();

myHandler.post(runn);

isPlaying = true;

}

});


pause.setOnClickListener(new View.OnClickListener() 

{   

public void onClick(View v) 

{

mediaPlayer.pause();


mediaPlayer.prepare();


}

});

seek.setProgress(mediaPlayer.getCurrentPosition());

System.out.println("**** MEDIA PLAYER CURRENT DURATION "+ mediaPlayer.getCurrentPosition());

}


private void init_mediaPlayer() 
    {

 System.gc();

final String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.SIZE };

          Log.e("media",""+proj);


musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);

 Log.e("media1",""+musiccursor);


 count = musiccursor.getCount();

Log.e("media2",""+count);

 if(musiccursor!=null)

{

musiccursor.moveToFirst();

MediaPlayer mediaPlayer = new  MediaPlayer();

do{

filename=musiccursor.getString(2);

Log.e("sdcard play",filename);

mediaPlayer.setDataSource(filename);

mediaPlayer.prepare();

if(mediaPlayer.getCurrentPosition()==mediaPlayer.getDuration())         
{                           
end=musiccursor.moveToNext();                       
}               
}               
while(end);         
}

in my app i am trying to play a media player along with a seek bar. When i tried to play the song from a folder stored in my app it was working fine and the seek bar get moved.

When i tried to play continuous songs from SD card it gets crashed and in logcat it showed the following error.

ERROR/sdcard play(2500): 02 - bird flu.mp3
ERROR/MediaPlayer(2500): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
ERROR/MediaPlayer(2500): error (1, -4)
ERROR/MediaPlayer(2500): Attempt to call getDuration without a valid mediaplayer
ERROR/MediaPlayer(2500): error (-38, 0)
ERROR/sdcard play(2500): 03 Un Mela Aasadhaan.mp3
ERROR/MediaPlayer(2500): setDataSource called in state 0
ERROR/MediaPlayer(2500): prepareAsync called in state 0
ERROR/MediaPlayer(2500): Attempt to call getDuration without a valid mediaplayer
ERROR/MediaPlayer(2500): error (-38, 0)

Following is my code

 sv = (SurfaceView)findViewById(R.id.surfaceView1);
//        mediaPlayer =MediaPlayer.create(this, R.raw.b);        
init_mediaPlayer();     


// sv.postDelayed(runnable,200);                
  seek = (SeekBar)findViewById(R.id.seekBar1);


 seek.setMax(mediaPlayer.getDuration());

System.out.println("**** MEDIA PLAYER DURATION "+ mediaPlayer.getDuration());       


play.setOnClickListener(new View.OnClickListener() 

{
public void onClick(View v) 

{

mediaPlayer.start();

myHandler.post(runn);

isPlaying = true;

}

});


pause.setOnClickListener(new View.OnClickListener() 

{   

public void onClick(View v) 

{

mediaPlayer.pause();


mediaPlayer.prepare();


}

});

seek.setProgress(mediaPlayer.getCurrentPosition());

System.out.println("**** MEDIA PLAYER CURRENT DURATION "+ mediaPlayer.getCurrentPosition());

}


private void init_mediaPlayer() 
    {

 System.gc();

final String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.SIZE };

          Log.e("media",""+proj);


musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);

 Log.e("media1",""+musiccursor);


 count = musiccursor.getCount();

Log.e("media2",""+count);

 if(musiccursor!=null)

{

musiccursor.moveToFirst();

MediaPlayer mediaPlayer = new  MediaPlayer();

do{

filename=musiccursor.getString(2);

Log.e("sdcard play",filename);

mediaPlayer.setDataSource(filename);

mediaPlayer.prepare();

if(mediaPlayer.getCurrentPosition()==mediaPlayer.getDuration())         
{                           
end=musiccursor.moveToNext();                       
}               
}               
while(end);         
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2024-11-14 06:36:14

以下是我的代码,我可以在移动搜索栏的情况下播放歌曲...

//SeekBar
媒体播放器 媒体播放器;
布尔值正在播放 = false;
SurfaceView sv;
//媒体播放器
光标音乐光标;
int music_column_index;
整数计数;
字符串文件名;
布尔结束=真;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
      setContentView(R.layout.start);
        sv = (SurfaceView)findViewById(R.id.surfaceView1);
    mediaPlayer =MediaPlayer.create(this, R.raw.blue);

    seekbar = (SeekBar)findViewById(R.id.seekBar1);
    seekbar.setMax(mediaPlayer.getDuration());
    System.out.println("**** MEDIA PLAYER DURATION "+ mediaPlayer.getDuration());

     Start = (Button)findViewById(R.id.widget306);
    Start.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View v) 
        {
            mediaPlayer.start(); //--> Start Media Palyer
            myHandler.post(runn); //--> SeekBar Progress
            isPlaying = true;
        }
    });
    seekbar.setProgress(mediaPlayer.getCurrentPosition());  
    seekbar = (SeekBar)findViewById(R.id.seekBar1);
}
 //SeekBar thread
 private Handler myHandler = new Handler();
 final Runnable runn = new Runnable()
 {
     public void run() 
    {
        if (mediaPlayer != null) 
        {
            if (isPlaying ) 
            {
               try 
               {
                     int currentPosition = mediaPlayer.getCurrentPosition();              
                     seekbar.setProgress(currentPosition);
               }
               catch (Exception e) 
               {
                        e.printStackTrace();
               }
               sv.postDelayed(runn, 150);
            }
          }
      }
 };

Following is my code, where i am able to play the song with a seek bar moving...

//SeekBar
MediaPlayer mediaPlayer;
boolean isPlaying = false;
SurfaceView sv;
//Media Player
Cursor musiccursor;
int music_column_index;
int count;
String filename;
boolean end=true;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
      setContentView(R.layout.start);
        sv = (SurfaceView)findViewById(R.id.surfaceView1);
    mediaPlayer =MediaPlayer.create(this, R.raw.blue);

    seekbar = (SeekBar)findViewById(R.id.seekBar1);
    seekbar.setMax(mediaPlayer.getDuration());
    System.out.println("**** MEDIA PLAYER DURATION "+ mediaPlayer.getDuration());

     Start = (Button)findViewById(R.id.widget306);
    Start.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View v) 
        {
            mediaPlayer.start(); //--> Start Media Palyer
            myHandler.post(runn); //--> SeekBar Progress
            isPlaying = true;
        }
    });
    seekbar.setProgress(mediaPlayer.getCurrentPosition());  
    seekbar = (SeekBar)findViewById(R.id.seekBar1);
}
 //SeekBar thread
 private Handler myHandler = new Handler();
 final Runnable runn = new Runnable()
 {
     public void run() 
    {
        if (mediaPlayer != null) 
        {
            if (isPlaying ) 
            {
               try 
               {
                     int currentPosition = mediaPlayer.getCurrentPosition();              
                     seekbar.setProgress(currentPosition);
               }
               catch (Exception e) 
               {
                        e.printStackTrace();
               }
               sv.postDelayed(runn, 150);
            }
          }
      }
 };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文