播放来自uri的音频

时间:2016-06-25 08:58:08

标签: android audio uri media

我尝试播放音频文件时收到#include <Windows.h> #include <iostream> #include <sstream> class COverlappedCompletionEvent : public OVERLAPPED { public: COverlappedCompletionEvent() : m_hEvent(NULL) { m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (m_hEvent == NULL) { auto nError = GetLastError(); std::stringstream ErrorStream; ErrorStream << "CreateEvent() failed with " << nError; throw std::runtime_error(ErrorStream.str()); } ZeroMemory(this, sizeof(OVERLAPPED)); hEvent = m_hEvent; } ~COverlappedCompletionEvent() { if (m_hEvent != NULL) { CloseHandle(m_hEvent); } } private: HANDLE m_hEvent; }; int main(int argc, char** argv) { try { if (argc != 2) { std::stringstream ErrorStream; ErrorStream << "usage: " << argv[0] << " <filename>"; throw std::runtime_error(ErrorStream.str()); } COverlappedCompletionEvent OverlappedCompletionEvent; char pBuffer[512]; auto hFile = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (hFile == NULL) { auto nError = GetLastError(); std::stringstream ErrorStream; ErrorStream << "CreateFileA() failed with " << nError; throw std::runtime_error(ErrorStream.str()); } if (ReadFile(hFile, pBuffer, sizeof(pBuffer), nullptr, &OverlappedCompletionEvent) == FALSE) { auto nError = GetLastError(); if (nError != ERROR_IO_PENDING) { std::stringstream ErrorStream; ErrorStream << "ReadFile() failed with " << nError; throw std::runtime_error(ErrorStream.str()); } } ::WaitForSingleObject(OverlappedCompletionEvent.hEvent, INFINITE); DWORD nBytesRead = 0; if (GetOverlappedResult(hFile, &OverlappedCompletionEvent, &nBytesRead, FALSE)) { std::cout << "Read " << nBytesRead << " bytes" << std::endl; } CloseHandle(hFile); } catch (const std::exception& Exception) { std::cout << Exception.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } 。 我尝试制作一个简单的应用程序,从手机内存中选择音频并使用java.lang.SecurityException e保存,并列出之前选择的那些。

在应用程序中,第一次播放音频文件时没有任何错误。但是在我重新启动应用程序后它不会播放而是返回sqlit 我使用下面的代码来选择音频文件

SecurityException.

我在清单文件中使用这些权限

       Intent intent = new Intent();
                intent.setType("audio/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Audio "), REQUEST_CODE);

  @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if(requestCode == REQUEST_CODE && data != null){
            Toast.makeText(MainActivity.this, "DATA " + data.getData(), Toast.LENGTH_SHORT).show();
            //Prompting alertdialog for name of the audio file           
           AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Enter name for audio");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(input);
            builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   sql.addToHistory(new soundDB.Effects(null,data.getData().toString(),input.getText().toString()));
               new loadEffects().execute();
                }
            });
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" />

2 个答案:

答案 0 :(得分:2)

  

但是在重新启动应用程序后,它不会播放而是返回SecurityException。

默认情况下,您只对进程的生命周期内Uri标识的内容拥有权限。可以将其视为与Web应用程序中的用户会话相关联的URL;一旦该会话超时,该URL将不再可用。

在API级别19+上,您可以尝试 using takePersistableUriPermission()以获得更持久的内容访问权限。但是,ACTION_OPEN_DOCUMENTACTION_GET_CONTENT更多,因此可能无效。

答案 1 :(得分:0)

我也有尝试用uri播放音频文件的问题。这就是我解决它的方法。获取音频文件uri时,我使用ACTION_OPEN_DOCUMENT而不是ACTION_GET_CONTENT,并将此行添加到onActivityResult。

&#13;
&#13;
this.getContentResolver().takePersistableUriPermission(currentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
&#13;
&#13;
&#13;

&#13;
&#13;
public void getAudioFileUri(View v) {

  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
  intent.setType("audio/*");
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  try {
    startActivityForResult(Intent.createChooser(intent, "Choose audio file"), 1);
  } catch (Exception ex) {

  }
}

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
  Uri currentUri = null;
  if (resultCode == Activity.RESULT_OK) {
    if (requestCode == 1) {
      if (resultData != null) {
        currentUri = resultData.getData();
        this.musicUri = currentUri.toString();
        this.getContentResolver().takePersistableUriPermission(currentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      }
    }
  }
}
&#13;
&#13;
&#13;