无法让 VIdeoView 进行随机播放

发布于 2025-01-02 03:10:04 字数 1587 浏览 1 评论 0原文

我在 android 中有一个视频视图,它正在从文本文件中读取行并播放它们。该文本文件包含指向网络服务器上的音频和视频文件的链接。然而,当它到达歌曲/视频的结尾时,它不会播放下一首。

看来我需要实现一个 OnCompletionListener 但我不知道该怎么做。这是正确的下降路径吗?

我尝试将其放入循环中执行播放方法 X 次,但它只是挂起。我假设它只是不断随机洗牌,直到到达循环末尾。

我还尝试使用 videoVIew.getDuration() 让它暂停音频/视频轨道的长度,

代码如下:

try {
         BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
         LineNumberReader rdr = new LineNumberReader(br);

          int min = 1; //Line 1 (start of file)
          int max = 20; // Line 20 (end of file)
          Random r = new Random();
          int someRandomNo = r.nextInt(max - min + 1) + min; 
          textView1.setText(Integer.toString(someRandomNo));
          // get random number - go to line - save it
          rdr.setLineNumber(someRandomNo);
          int linenum = 0;
          String theLine = "";
          while (linenum < someRandomNo) {
              theLine = br.readLine();
              linenum++;
          }
          textView1.setText(theLine);

              VideoView videoView = (VideoView) findViewById(R.id.VideoView);         
              MediaController mediaController = new MediaController(this);
              mediaController.setAnchorView(videoView);
      // Set Audio/Video to play
              Uri video = Uri.parse(theLine);
              videoView.setMediaController(mediaController);
              videoView.setVideoURI(video);
              videoView.start();                                      
      }
      catch (Exception e) {

      }

I have a videoview in android that is reading lines from a text file and playing them. The text file contains links to audio and video files on a webserver. However when it reaches the end of the song/video it will not play the next one.

It seems that I need to implement an OnCompletionListener but I have no idea how to do that. Would that be the correct path to go down?

I have tried putting it in a loop to perform the play method X amount of times but it just hangs. I am assuming it just keeps shuffling randomly until it reaches the end of the loop.

I have also attempted to get it to pause for the length of the audio/video track by using videoVIew.getDuration()

The code is below:

try {
         BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
         LineNumberReader rdr = new LineNumberReader(br);

          int min = 1; //Line 1 (start of file)
          int max = 20; // Line 20 (end of file)
          Random r = new Random();
          int someRandomNo = r.nextInt(max - min + 1) + min; 
          textView1.setText(Integer.toString(someRandomNo));
          // get random number - go to line - save it
          rdr.setLineNumber(someRandomNo);
          int linenum = 0;
          String theLine = "";
          while (linenum < someRandomNo) {
              theLine = br.readLine();
              linenum++;
          }
          textView1.setText(theLine);

              VideoView videoView = (VideoView) findViewById(R.id.VideoView);         
              MediaController mediaController = new MediaController(this);
              mediaController.setAnchorView(videoView);
      // Set Audio/Video to play
              Uri video = Uri.parse(theLine);
              videoView.setMediaController(mediaController);
              videoView.setVideoURI(video);
              videoView.start();                                      
      }
      catch (Exception e) {

      }

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

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

发布评论

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

评论(2

似梦非梦 2025-01-09 03:10:04

这就是你想要的:

viedoView.setOnCompletionListener(MediaPlayer.OnCompletionListener l);

以及 android dev 上的 onCompletionListener 资源 - http:// /developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html

就其价值而言,我建议您对文件执行一次 IO,并将 Line 保存在数组中或ArrayList,这样您就可以访问它,甚至可以创建一个自定义类来处理洗牌。例如,您可以让它跟踪已发送的字符串,即已播放的视频,然后随机选择一个尚未播放的新视频。

This is what you want:

viedoView.setOnCompletionListener(MediaPlayer.OnCompletionListener l);

And the onCompletionListener resource on android dev- http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html

And for what its worth, I would suggest you do your IO with the file once, and save theLine in an array or ArrayList and that way you can access it, or even make a custom class to handle shuffling. For example you could have it keep track of which strings had been sent, ie which videos has been played, and then randomly choose a new one that had not been played yet.

赠我空喜 2025-01-09 03:10:04

AJcodez 的答案正是我所需要的,我只是不确定如何使用 OnCompletion Listener。

我将 OnCreate 代码放入其自己的方法中,然后让侦听器每次都调用它。我也没有为该类实现 MediaPayer.OnCompletition 监听器。

//Implement the listener
public class CardioActivity extends Activity implements MediaPlayer.OnCompletionListener {

//Oncreate calls play
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        play();
}

 public void play() {
                    //Blah blah all other code is here
                    //Set the listener etc like this
                videoView.setMediaController(mediaController);
                videoView.setVideoURI(video);
                videoView.setOnCompletionListener(this);                    
                videoView.start();              
        }
        catch (Exception e) {
         //Handling exceptions
        }
    }

@Override
public void onCompletion(MediaPlayer arg0) {
    //Recall the method
    play();
}

我遇到的另一个 SO 链接

AJcodez answer was what I needed, I was just unsure how to use the OnCompletion Listener.

I put my OnCreate code into its own method and then simply had the listener call it everytime. I also wasn't implementing the MediaPayer.OnCompletition Listener for the class.

//Implement the listener
public class CardioActivity extends Activity implements MediaPlayer.OnCompletionListener {

//Oncreate calls play
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        play();
}

 public void play() {
                    //Blah blah all other code is here
                    //Set the listener etc like this
                videoView.setMediaController(mediaController);
                videoView.setVideoURI(video);
                videoView.setOnCompletionListener(this);                    
                videoView.start();              
        }
        catch (Exception e) {
         //Handling exceptions
        }
    }

@Override
public void onCompletion(MediaPlayer arg0) {
    //Recall the method
    play();
}

Another SO link that I came across

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文