【问题标题】:youtubeExtractor for Android适用于 Android 的 youtubeExtractor
【发布时间】:2018-07-14 01:48:40
【问题描述】:

我想将 youtubeExtractor 用于 android 应用程序,我找到了类似的库

compile 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'

github中有一个示例代码我复制了它。我打开应用程序后,它同时关闭。电话和Logcat都没有错误。只有有 W/System:ClassLoader 引用了未知路径:logcat 上的 /data/app/oz.videos-1/lib/arm64。我不确定这是否是一个错误。有什么建议吗?提前致谢。

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import at.huber.youtubeExtractor.VideoMeta;
import at.huber.youtubeExtractor.YouTubeExtractor;
import at.huber.youtubeExtractor.YtFile;

public class MainActivity extends Activity
{

private static String youtubeLink;

private LinearLayout mainLayout;
private ProgressBar mainProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    mainProgressBar = (ProgressBar) findViewById(R.id.prgrBar);

    // Check how it was started and if we can get the youtube link
    if (savedInstanceState == null && Intent.ACTION_SEND.equals(getIntent().getAction())
            && getIntent().getType() != null && "text/plain".equals(getIntent().getType()))
    {

        String ytLink = getIntent().getStringExtra(Intent.EXTRA_TEXT);

        if (ytLink != null
                && (ytLink.contains("://youtu.be/") || ytLink.contains("youtube.com/watch?v=")))
        {
            youtubeLink = ytLink;
            // We have a valid link
            getYoutubeDownloadUrl(youtubeLink);
        }
        else
        {
            Toast.makeText(this, R.string.error_no_yt_link, Toast.LENGTH_LONG).show();
            finish();
        }
    } else if (savedInstanceState != null && youtubeLink != null) {
        getYoutubeDownloadUrl(youtubeLink);
    } else {
        finish();
    }
}

private void getYoutubeDownloadUrl(String youtubeLink)
{
    new YouTubeExtractor(this)
    {

        @Override
        public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta vMeta) {
            mainProgressBar.setVisibility(View.GONE);

            if (ytFiles == null) {
                // Something went wrong we got no urls. Always check this.
                finish();
                return;
            }
            // Iterate over itags
            for (int i = 0, itag; i < ytFiles.size(); i++) {
                itag = ytFiles.keyAt(i);
                // ytFile represents one file with its url and meta data
                YtFile ytFile = ytFiles.get(itag);

                // Just add videos in a decent format => height -1 = audio
                if (ytFile.getFormat().getHeight() == -1 || ytFile.getFormat().getHeight() >= 360) {
                    addButtonToMainLayout(vMeta.getTitle(), ytFile);
                }
            }
        }
    }.extract(youtubeLink, true, false);
}

private void addButtonToMainLayout(final String videoTitle, final YtFile ytfile)
{
    // Display some buttons and let the user choose the format
    String btnText = (ytfile.getFormat().getHeight() == -1) ? "Audio " +
            ytfile.getFormat().getAudioBitrate() + " kbit/s" :
            ytfile.getFormat().getHeight() + "p";
    btnText += (ytfile.getFormat().isDashContainer()) ? " dash" : "";
    Button btn = new Button(this);
    btn.setText(btnText);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String filename;
            if (videoTitle.length() > 55) {
                filename = videoTitle.substring(0, 55) + "." + ytfile.getFormat().getExt();
            } else {
                filename = videoTitle + "." + ytfile.getFormat().getExt();
            }
            filename = filename.replaceAll("\\\\|>|<|\"|\\||\\*|\\?|%|:|#|/", "");
            downloadFromUrl(ytfile.getUrl(), videoTitle, filename);
            finish();
        }
    });
    mainLayout.addView(btn);
}

private void downloadFromUrl(String youtubeDlUrl, String downloadTitle, String fileName)
{
    Uri uri = Uri.parse(youtubeDlUrl);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setTitle(downloadTitle);

    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}
}

【问题讨论】:

    标签: android youtube path extractor


    【解决方案1】:

    可能是您的savedInstanceState == null,然后调用了finish() 方法。 尝试在mainProgressBar = (ProgressBar) findViewById(R.id.prgrBar); 之后使用 youtube 链接致电getYoutubeDownloadUrl()

    【讨论】:

      【解决方案2】:

      请尝试实现2.0.0版本的库: implementation 'com.github.HaarigerHarald:android-youtubeExtractor:v2.0.0'

      【讨论】:

        猜你喜欢
        • 2012-01-27
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-21
        • 2011-09-17
        • 2012-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多