草庐IT

android - 如何为大型内容创建流媒体视频播放器?

coder 2023-11-27 原文

我花了几个小时在 android 中开发一个合适的流媒体视频播放器,并且非常成功地创建了一个可以很好地播放诸如歌曲、预告片等小内容的播放器。但是播放器对于电影和电视节目等大型内容表现出一些异常行为,因为它需要大量流式传输,播放器开始滞后于此类数据。谁能帮我破解这样一个问题的解决方案。

提前致谢...

这是来源:

public class player extends Activity implements OnErrorListener,
    OnPreparedListener {



/** Called when the activity is first created. */
private static final int UPDATE_FREQUENCY = 500;

private static final int STEP_VALUE = 4000;

private static final int DIALOG_KEY = 0;

private TextView currentTime, duration;

private VideoView videoView;

private SeekBar seekbar = null;

private View mediacontroller;

private ProgressDialog progressDialog = null;

private ImageButton playButton = null;

private ImageButton prevButton = null;

private ImageButton nextButton = null;

private boolean isMoveingSeekBar = false;

private boolean isMediaCtrlShown = false;

private final Handler handler = new Handler();

private boolean isStarted = true;

private String currentFile = "singham_320b";

private boolean isCustomSeekButtonClick = false;

private boolean isPauseButtonClick = false;

private static boolean isMyDialogShowing = false;

private static int percentageBuffer = 0; 

private int mpCurrentPosition;

int hh = 00, mm = 00, ss = 00, ms = 00;

int i = 0;

int previouPosition = 0;

private Runnable onEverySecond=new Runnable() {
      public void run() {
              if (videoView!=null) {
                      seekbar.setProgress(videoView.getCurrentPosition());
              }

              if (!isPauseButtonClick) {
                      mediacontroller.postDelayed(onEverySecond, 1000);
              }
      }
};

private final Runnable updatePositionRunnable = new Runnable()
{
    public void run() 
    {
        updatePosition();
    }
};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    setUpMyDialog();
    showMyDialog();

    videoView = (VideoView) findViewById(R.id.videoview);

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    seekbar = (SeekBar) findViewById(R.id.seekbar);

    currentTime = (TextView) findViewById(R.id.currentTime);

    playButton = (ImageButton) findViewById(R.id.play);

    prevButton = (ImageButton) findViewById(R.id.prev);

    nextButton = (ImageButton) findViewById(R.id.next);

    duration = (TextView) findViewById(R.id.duration);

    mediacontroller = findViewById(R.id.mediacontroller);


    videoView.setOnErrorListener(this);

    videoView.setOnPreparedListener(this);

    videoView.setOnTouchListener(new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {

            if (!isMediaCtrlShown) 
            {
                mediacontroller.setVisibility(View.GONE);

                isMediaCtrlShown = true;

            } 
            else 
            {
                mediacontroller.setVisibility(View.VISIBLE);

                isMediaCtrlShown = false;
            }
            return false;
        }
    });

    Uri video = Uri.parse("http://wpc.1B42.edgecastcdn.net/001B42/mobile/songs/pyaar_ka_punchnama/life_sahi_hai_320b.mp4");

    videoView.setVideoURI(video);

    seekbar.setOnSeekBarChangeListener(seekBarChanged);

    playButton.setOnClickListener(onButtonClick);

    nextButton.setOnClickListener(onButtonClick);

    prevButton.setOnClickListener(onButtonClick);

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    return false;
}

public void calculateTime(int ms) {

    ss = ms / 1000;

    mm = ss / 60;

    ss %= 60;

    hh = mm / 60;

    mm %= 60;

    hh %= 24;

}

@Override
public void onPrepared(MediaPlayer mp)
{
    dismissMyDialog();

    videoView.start();

    mediacontroller.setVisibility(View.VISIBLE);

    isMediaCtrlShown = false;

    seekbar.setProgress(0);

    seekbar.setMax(videoView.getDuration());


    ms = videoView.getDuration();

    calculateTime(ms);

    duration.setText("" + hh + ":" + mm + ":" + ss);

    ms = videoView.getCurrentPosition();

    calculateTime(ms);

    currentTime.setText("" + hh + ":" + mm + ":" + ss);

    playButton.setImageResource(android.R.drawable.ic_media_pause);

    updatePosition();

    isStarted = true;

    mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
    {
        // show updated information about the buffering progress
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent)
        {
            Log.d(this.getClass().getName(), "percent: " + percent);
            percentageBuffer = percent;
            secondarySeekBarProgressUpdater(percent);
            // progress.setSecondaryProgress(percent);
            if (i == 0)
            {
                i = i + 1;

                previouPosition = mp.getCurrentPosition();
            }
            else if (i == 1)
            {
                if (mp.getCurrentPosition() == previouPosition)
                {
                    if (!isPauseButtonClick)
                    {

                        showMyDialog();
                        if (percent == 100)
                        {
                            dismissMyDialog();
                        }
                    }
                }
                else
                {
                    i = 0;

                    previouPosition = 0;

                    dismissMyDialog();
                }
            }
            else if (isCustomSeekButtonClick)
            {
                isCustomSeekButtonClick = false;

                if (mpCurrentPosition == mp.getCurrentPosition())
                {

                    showMyDialog();
                    if (percent == 100)
                    {
                        dismissMyDialog();
                    }
                }
                else
                {
                    dismissMyDialog();
                }
            }
        }
    });

    mp.setOnSeekCompleteListener(new OnSeekCompleteListener()
    {
        public void onSeekComplete(MediaPlayer mp) 
        {
            if (mp.isPlaying())
            {

            }
            else 
            {
                onStart();

                onPause();

                onStart();

            }

        }
    });
}


private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener()
{
    @Override
    public void onStopTrackingTouch(SeekBar seekBar)
    {
        isMoveingSeekBar = false;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar)
    {
        isMoveingSeekBar = true;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) 
    {
        Log.e("",""+progress+""+percentageBuffer);

        if (fromUser)
        {
            isCustomSeekButtonClick = fromUser;

            videoView.seekTo(progress);

            mpCurrentPosition = progress;

            Log.e("OnSeekBarChangeListener", "onProgressChanged");
        }
        if (isMoveingSeekBar) 
        {
            videoView.seekTo(progress);

            Log.i("OnSeekBarChangeListener", "onProgressChanged");
        }
    }
};
private View.OnClickListener onButtonClick = new View.OnClickListener() {

    @Override
    public void onClick(View v)
    {
        switch (v.getId()) 
        {
        case R.id.play:
        {
            if (videoView.isPlaying())
            {
                handler.removeCallbacks(updatePositionRunnable);

                isPauseButtonClick = true;

                videoView.pause();

                playButton.setImageResource(android.R.drawable.ic_media_play);

            } 
            else 
            {
                if (isStarted)
                {
                    videoView.start();
                    isPauseButtonClick = false;
                    playButton.setImageResource(android.R.drawable.ic_media_pause);

                    updatePosition();
                } 
                else 
                {
                    startPlay(currentFile);
                    isPauseButtonClick = false;
                    videoView.start();
                }
            }

            break;
        }
        case R.id.next: 
        {
            int seekto = videoView.getCurrentPosition() + STEP_VALUE;

            if (seekto > videoView.getDuration())

                seekto = videoView.getDuration();

            videoView.pause();

            videoView.seekTo(seekto);
            /*
             * try { Thread.sleep(15000); } catch (InterruptedException e) {
             * // TODO Auto-generated catch block e.printStackTrace(); }
             */
            // player.pause();
            videoView.start();

            break;
        }
        case R.id.prev: {
            int seekto = videoView.getCurrentPosition() - STEP_VALUE;

            if (seekto < 0)
                seekto = 0;

            videoView.pause();

            videoView.seekTo(seekto);

            // player.pause();
            videoView.start();

            break;
        }
        }
    }
};

private void updatePosition() 
{
    handler.removeCallbacks(updatePositionRunnable);

    seekbar.setProgress(videoView.getCurrentPosition());

    ms = videoView.getCurrentPosition();

    calculateTime(ms);

    currentTime.setText("" + hh + ":" + mm + ":" + ss);

    handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
}

private void startPlay(String file) 
{
    Log.i("Selected: ", file);

    // selelctedFile.setText(file);
    seekbar.setProgress(0);

    videoView.stopPlayback();

    videoView.start();

    seekbar.setMax(videoView.getDuration());

    playButton.setImageResource(android.R.drawable.ic_media_pause);

    updatePosition();

    isStarted = true;
}
void setUpMyDialog()
{
    if (progressDialog == null)
    {
        progressDialog = (ProgressDialog) onCreateDialog(DIALOG_KEY);

        progressDialog = new ProgressDialog(player.this);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    }
}

void showMyDialog()
{
    Log.e("showMyDialog***", "****" + isMyDialogShowing);

    if (!isMyDialogShowing)
    {
        isMyDialogShowing = true;

        Log.e("showMyDialog: true***", "****" + isMyDialogShowing);

        if (progressDialog != null && !progressDialog.isShowing())
        {
            Log.e("showMyDialog: true***", "****progressDialog" + progressDialog.isShowing());

            progressDialog.show();
        }
        else if(progressDialog == null)
        {
            setUpMyDialog();
            progressDialog.show();
        }
    }
}

void dismissMyDialog()
{
    Log.e("dismissMyDialog***", "****");

    if (progressDialog != null && progressDialog.isShowing())
    {
        progressDialog.dismiss();
        progressDialog = null;

        isMyDialogShowing = false;
    }
}

void killMyDialog()
{
    isMyDialogShowing = false;
}
private void secondarySeekBarProgressUpdater(int percent){
    seekbar.setSecondaryProgress(percent);
}

最佳答案

我的直觉告诉您,您可能会因为专注于播放器而忽略了一些东西,特别是因为它适用于较小的内容。您是否考虑过检查流式传输服务器?如果服务器不能胜任传输较大文件的任务,那么播放器就无能为力了。此外,您可以调整来自服务器的数据包大小,以帮助播放器一次使用较小的“咬合”(请原谅双关语)媒体来管理播放。尝试使用免费的 Apple Darwin 流媒体服务器。有一个适用于 Apache、Windows 和其他版本的版本,而且它的配置性很强。给自己准备一组不同大小的文件,并尝试确定在什么大小播放开始失败。该数字将为您提供有关问题所在的重要线索,无论是传输的服务器数据包大小、Android 环境中的可用内存还是其他地方。无论数字如何,请尝试从服务器设置较小的数据包大小。这应该可以让您的播放器减少工作量,并有望改善播放效果。

您可以从网上的许多地方获得 Darwin 服务器。 Here is one such link .维基百科也有一些有用的信息和此服务器的一些链接,find them here .祝你在这方面的研究顺利。

弗兰克。

关于android - 如何为大型内容创建流媒体视频播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230382/

有关android - 如何为大型内容创建流媒体视频播放器?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  7. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

  8. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  9. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  10. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

随机推荐